package com.phpxiu.app.view.custom;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.TextView;

import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.gift.GiftAnim;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by WuJinZhou on 2016/5/19.
 */
public class GiftAnimView extends BaseAnimView{
    private final SpannableStringBuilder GIFT_NAME=new SpannableStringBuilder();
    private final ForegroundColorSpan GIFT_NAME_SPAN=new ForegroundColorSpan(Color.parseColor("#ff31DFE1"));
    public final static int STARTED=0x1;//开始计数
    public final static int WAIT_COUNT=0x2;//等待计数
    public final static int ANIM_COUNT=0x3;//正在计数
    public final static int COUNT_FINISH=0x4;//计数结束
    public final static int HIDE=0x5;//隐藏动画视图
    private String key;
    private GiftAnim giftAnim;
    private FrescoImageView headView;
    private TextView nickView;
    private TextView msgView;
    private FrescoImageView iconView;
    public  Animation enterAnim,outAnim;
    public AnimationSet bigNumAnim,smallNumAnim,smallNumAnim_;//文字缩放动画

    public TextView countView;
    public int state=HIDE;
    private AtomicInteger count=new AtomicInteger(1);
    private AtomicInteger animCount=new AtomicInteger(0);
    public GiftAnimView(Context context) {
        super(context);
    }
    private String operaLabel="Gift a";

    public GiftAnimView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GiftAnimView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(21)
    public GiftAnimView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    /**
     * 初始化
     */
    private void init(){
        addView(LayoutInflater.from(getContext()).inflate(R.layout.common_gift_small_anim_view,null));
        headView=(FrescoImageView)findViewById(R.id.gift_anim_view_head_view);
        nickView=(TextView)findViewById(R.id.user_nick);
        msgView=(TextView)findViewById(R.id.gift_info);
        iconView=(FrescoImageView)findViewById(R.id.gift_icon_view);
        countView=(TextView)findViewById(R.id.gift_count_view);
        initNumAnim();
        enterAnim=AnimationUtils.loadAnimation(getContext(), R.anim.common_gift_enter);
        outAnim=AnimationUtils.loadAnimation(getContext(), R.anim.common_gift_out);
    }

    /**
     * 初始化计数动画
     */
    private void initNumAnim(){
        bigNumAnim = new AnimationSet(true);
        bigNumAnim.addAnimation(new ScaleAnimation(1.0f, 2f, 1.0f, 2f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f));
        bigNumAnim.setDuration(300);
        bigNumAnim.setInterpolator(new AccelerateInterpolator());
        bigNumAnim.setFillAfter(true);
        bigNumAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }
        });
        smallNumAnim = new AnimationSet(true);
        smallNumAnim.addAnimation(new ScaleAnimation(2f, 1.0f, 2f,
                1.0f, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f));
        smallNumAnim.setDuration(300);
        smallNumAnim.setInterpolator(new DecelerateInterpolator());
        smallNumAnim.setFillAfter(false);
        smallNumAnim_ = new AnimationSet(true);
        smallNumAnim_.addAnimation(new ScaleAnimation(2f, 1.0f, 2f,
                1.0f, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f));
        smallNumAnim_.setDuration(300);
        smallNumAnim_.setInterpolator(new DecelerateInterpolator());
        smallNumAnim_.setFillAfter(false);
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public GiftAnim getGiftAnim() {
        return giftAnim;
    }

    public void setGiftAnim(GiftAnim giftAnim) {
        count.getAndSet(giftAnim.getMsgList().size());
        this.giftAnim = giftAnim;
    }

    /**
     * 更新显示信息
     */
    public void updateInfo(String head,String nick,String giftName,String icon){
        if(head!=null) {
            headView.setImageURI(Uri.parse(head));
        }
        nickView.setText(nick);
        GIFT_NAME.clear();
        GIFT_NAME.append(operaLabel).append(giftName);
        GIFT_NAME.setSpan(GIFT_NAME_SPAN,operaLabel.length(),GIFT_NAME.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msgView.setText(GIFT_NAME);
        if(icon!=null) {
            iconView.setImageURI(Uri.parse(icon));
        }
    }

    public void enter(){
        clearAnimation();
        startAnimation(enterAnim);
    }

    public int getCount() {
        int c=count.get();
        return c;
    }

    public void setCount() {
        this.count.incrementAndGet();//自增1
    }

    public int getAnimCount() {
        int c=animCount.get();
        return c;
    }

    /**
     * 变数
     */
    public boolean changeNum(){
        //如果当前计数值小于总数值
        if(animCount.get()<count.get()){
            countView.setText("x"+animCount.incrementAndGet());
            countView.startAnimation(smallNumAnim);
            return false;
        }
        return true;//通知等待
    }

    /**
     * 计数开始
     */
    public void startCountAnim(){
        int count=0;
        if(animCount.get()==0){
            count=animCount.addAndGet(1);
        }else{
            count=animCount.get();
        }
        countView.setText("x"+count);
        countView.startAnimation(bigNumAnim);
    }

    public void resetAnim(){
        countView.startAnimation(smallNumAnim_);
    }
    /**
     * 重置每一次播完计数动画延迟3秒重置
     * 1.isPrepare 置为fase 此退出动画开始
     */
    public void finish(){
        key=null;
        count.getAndSet(0);
        animCount.getAndSet(0);
        this.giftAnim=null;
        clearAnimation();
        startAnimation(outAnim);
    }

    public void hide(){
        headView.setImageURI(Uri.parse("res:///"+R.drawable.def_user_icon));
        nickView.setText("--");
        msgView.setText("--");
        countView.setText("");
    }

    public void setOperaLabel(String operaLabel) {
        this.operaLabel = operaLabel;
    }
}
